﻿#region Software License
/**************************************************************/
/*          Business of Software Consulting                   */
/*          FREE SOFTWARE LICENSE                             */
/**************************************************************/
/*  Directory Manifest Generator                              */
/*  by: Mike Cating                                           */
/*  © 2009 Business of Software Consulting                    */
/*                                                            */
/* BUSINESS OF SOFTWARE CONSULTING DISCLAIMS ALL WARRANTIES,  */
/* RESPONSIBILITIES, AND LIABILITIES ASSOCIATED WITH          */
/* USE OF THIS CODE IN ANY WAY, SHAPE, OR FORM                */
/* REGARDLESS HOW IMPLICIT, EXPLICIT, OR OBSCURE IT           */
/* IS. IF THERE IS ANYTHING QUESTIONABLE WITH REGARDS         */
/* TO THIS SOFTWARE BREAKING AND YOU GAIN A LOSS OF           */
/* ANY NATURE, WE ARE NOT THE RESPONSIBLE PARTY. USE          */
/* OF THIS SOFTWARE CREATES ACCEPTANCE OF THESE TERMS         */
/*                                                            */
/* USE OF THIS CODE MUST RETAIN ALL COPYRIGHT NOTICES         */
/* AND LICENSES (MEANING THIS TEXT).                          */
/*                                                            */
/**************************************************************/
#endregion


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Globalization;
using System.Xml.Serialization;
using System.Diagnostics.CodeAnalysis;

namespace BOSTools.DirectoryManifestGenerator
{
    [SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
    public class XmlManifestWriter
    {
        private string _outputFile;
        DirectoryManifest _manifest = new DirectoryManifest();

        public XmlManifestWriter(string sourceDir, string outputFile)
        {
            if (string.IsNullOrEmpty(sourceDir))
            {
                throw new ArgumentException("sourceDir was null or empty");
            }

            if (string.IsNullOrEmpty(outputFile))
            {
                throw new ArgumentException("outputFile was null or empty");
            }

            _manifest.DirectoryName = sourceDir;
            _outputFile = outputFile;
        }

        public void AddFileDetail(FileDetail detail)
        {
            if (detail == null)
            {
                throw new ArgumentNullException("detail");
            }

            _manifest.AddFileDetail(detail);
        }

        public void AddDirectory(string directoryName)
        {
            _manifest.AddDirectory(directoryName);
        }

        public void Close()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DirectoryManifest));
            using (StreamWriter writer = File.CreateText( _outputFile ))
            {
                serializer.Serialize(writer, _manifest);
            }
        }
    }
}
